/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Presentacion;

import ManejoExc.MateriaPrimaException;
import Negocio.Agricola;
import Negocio.Cliente;
import Negocio.Deposito;
import Negocio.Empresa;
import Negocio.Ingreso;
import Negocio.LeniaSeca;
import Negocio.LeniaVerde;
import Negocio.Parcela;
import Negocio.Personal;
import Negocio.Plantacion;
import Negocio.Seccion;
import Negocio.TipoMadera;
import Negocio.YerbaMateCanchada;
import Negocio.YerbaMateVerde;
import java.sql.Date;
import java.sql.Time;
import java.util.Enumeration;
import java.util.Iterator;
import javax.swing.AbstractButton;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;

/**
 *
 * @author robert
 */
public class DlgIngreso extends javax.swing.JDialog {

    private DefaultComboBoxModel<Personal> choferes = new DefaultComboBoxModel();
    private DefaultComboBoxModel<Agricola> vehiculos = new DefaultComboBoxModel();
    private DefaultComboBoxModel<TipoMadera> tipomadera = new DefaultComboBoxModel();
    private DefaultComboBoxModel<Parcela> parcelas = new DefaultComboBoxModel();
    private DefaultComboBoxModel<Plantacion> plantaciones = new DefaultComboBoxModel();
    private DefaultListModel<Ingreso> ingresos = new DefaultListModel<>();
    private DefaultComboBoxModel<Cliente> clientes = new DefaultComboBoxModel();
    private Empresa miEmpresa;
    private Ingreso unIngreso;

    /**
     * Creates new form jDlgIngreso
     */
    public DlgIngreso(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    public DlgIngreso(java.awt.Frame parent, boolean modal, Empresa unaEmpresa) {
        super(parent, modal);
        this.miEmpresa = unaEmpresa;
        initComponents();
        this.jComboChofer.setModel(this.choferes);
        this.jComboVehiculo.setModel(this.vehiculos);
        this.jComboTipoMadera.setModel(this.tipomadera);
        this.jComboPlantacion.setModel(this.plantaciones);
        this.jComboParcela.setModel(this.parcelas);
        this.jComboCliente.setModel(this.clientes);
        this.jListIngresos.setModel(ingresos);
        this.cargarIngresos();
        this.cargarChoferes();
        this.cargarVehiculos();
        this.cargarTiposMadera();
        this.cargarPlantaciones();
        this.cargarClientes();
    }

    private void cargarClientes() {
        //this.modeloNumerosTel.clear();
        this.clientes.removeAllElements();
        if (this.miEmpresa.getClientesActivos().size() > 0) { //Si La lista no tiene elementos
            Iterator it = this.miEmpresa.getClientesActivos().iterator();
            while (it.hasNext()) {
                this.clientes.addElement((Cliente) it.next());
            }
        } else {
            //this.choferes.addElement("No se encontró ningún personal.");
        }

    }

    private void cargarChoferes() {
        //this.modeloNumerosTel.clear();
        this.choferes.removeAllElements();
        if (this.miEmpresa.getEmpleadosActivos().size() > 0) { //Si La lista no tiene elementos
            Iterator it = this.miEmpresa.getEmpleadosActivos().values().iterator();
            while (it.hasNext()) {
                this.choferes.addElement((Personal) it.next());
            }
        } else {
            //this.choferes.addElement("No se encontró ningún personal.");
        }

    }

    private void cargarIngresos() {
        //this.modeloNumerosTel.clear();
        this.ingresos.removeAllElements();
        if (0 <= this.miEmpresa.getIngresos().size()) { //Si La lista no tiene elementos
            Iterator it = this.miEmpresa.getIngresos().iterator();
            while (it.hasNext()) {
                this.ingresos.addElement((Ingreso) it.next());
            }
        } else {
            //this.choferes.addElement("No se encontró ningún personal.");
        }

    }

    private void cargarVehiculos() {
        //this.modeloNumerosTel.clear();
        this.vehiculos.removeAllElements();
        if (this.miEmpresa.getAgricolas().size() > 0) { //Si La lista no tiene elementos
            Iterator it = this.miEmpresa.getAgricolas().values().iterator();
            while (it.hasNext()) {
                this.vehiculos.addElement((Agricola) it.next());
            }
        } else {
            //this.choferes.addElement("No se encontró ningún personal.");
        }

    }

    private void cargarTiposMadera() {
        //this.modeloNumerosTel.clear();
        this.tipomadera.removeAllElements();
        if (this.miEmpresa.getTiposmaderas().size() > 0) { //Si La lista no tiene elementos
            Iterator it = this.miEmpresa.getTiposmaderas().iterator();
            while (it.hasNext()) {
                this.tipomadera.addElement((TipoMadera) it.next());
            }
        } else {
            //this.choferes.addElement("No se encontró ningún personal.");
        }

    }

    private void cargarPlantaciones() {
        //this.modeloNumerosTel.clear();
        this.plantaciones.removeAllElements();
        if (this.miEmpresa.getPlantaciones().size() > 0) { //Si La lista no tiene elementos
            Iterator it = this.miEmpresa.getPlantaciones().iterator();
            while (it.hasNext()) {
                this.plantaciones.addElement((Plantacion) it.next());
            }
        } else {
            //this.choferes.addElement("No se encontró ningún personal.");
        }

    }

    /*
     private void cargarParcelas() {
     //this.modeloNumerosTel.clear();
     this.parcela.removeAllElements();
     if (this.miEmpresa.getParcelas().size() > 0) { //Si La lista no tiene elementos
     Iterator it = this.miEmpresa.getParcelas().values().iterator();
     while (it.hasNext()) {
     this.parcela.addElement((Parcela) it.next());
     }
     } else {
     //this.choferes.addElement("No se encontró ningún personal.");
     }

     }
     */
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnGroupOrigen = new javax.swing.ButtonGroup();
        btnGroupTipoProducto = new javax.swing.ButtonGroup();
        jScrollPane1 = new javax.swing.JScrollPane();
        jListIngresos = new javax.swing.JList();
        jLabel1 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        dateFecha = new datechooser.beans.DateChooserCombo();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jRadioPropio = new javax.swing.JRadioButton();
        jRadioTercero = new javax.swing.JRadioButton();
        jLabel4 = new javax.swing.JLabel();
        jTextHora = new javax.swing.JFormattedTextField();
        jLabel5 = new javax.swing.JLabel();
        jRadioYerbaMateVerde = new javax.swing.JRadioButton();
        jRadioYerbaCanchada = new javax.swing.JRadioButton();
        jRadioLeñaVerde = new javax.swing.JRadioButton();
        jRadioLeñaSeca = new javax.swing.JRadioButton();
        jLabel6 = new javax.swing.JLabel();
        jComboChofer = new javax.swing.JComboBox();
        jLabel7 = new javax.swing.JLabel();
        jComboVehiculo = new javax.swing.JComboBox();
        jLabel9 = new javax.swing.JLabel();
        jTextHumedad = new javax.swing.JFormattedTextField();
        jLabel10 = new javax.swing.JLabel();
        jTextPalo = new javax.swing.JFormattedTextField();
        jLabel11 = new javax.swing.JLabel();
        jTextPolvo = new javax.swing.JFormattedTextField();
        jSeparator2 = new javax.swing.JSeparator();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        jSeparator3 = new javax.swing.JSeparator();
        jTextPesoBruto = new javax.swing.JFormattedTextField();
        jLabel14 = new javax.swing.JLabel();
        jTextTara = new javax.swing.JTextField();
        jTextKilajeTotal = new javax.swing.JFormattedTextField();
        jLabel15 = new javax.swing.JLabel();
        jComboTipoMadera = new javax.swing.JComboBox();
        jBtnCerrar = new javax.swing.JButton();
        jBtnGuardar = new javax.swing.JButton();
        jBtnNuevo = new javax.swing.JButton();
        jLabel16 = new javax.swing.JLabel();
        jComboPlantacion = new javax.swing.JComboBox();
        jLabel17 = new javax.swing.JLabel();
        jComboParcela = new javax.swing.JComboBox();
        jLabel8 = new javax.swing.JLabel();
        jLabel18 = new javax.swing.JLabel();
        jLabel19 = new javax.swing.JLabel();
        jLabel20 = new javax.swing.JLabel();
        jLabel21 = new javax.swing.JLabel();
        jLabel22 = new javax.swing.JLabel();
        jLabel23 = new javax.swing.JLabel();
        jComboCliente = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Ingresos");
        setModal(true);
        setResizable(false);

        jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

        jListIngresos.setPreferredSize(new java.awt.Dimension(790, 560));
        jListIngresos.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                jListIngresosValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(jListIngresos);

        jLabel1.setText("Listado de Ingresos");

        jSeparator1.setForeground(new java.awt.Color(0, 0, 0));
        jSeparator1.setOpaque(true);

        dateFecha.setBehavior(datechooser.model.multiple.MultyModelBehavior.SELECT_SINGLE);

        jLabel2.setLabelFor(dateFecha);
        jLabel2.setText("Fecha");

        jLabel3.setText("Origen");

        btnGroupOrigen.add(jRadioPropio);
        jRadioPropio.setText("Propio");
        jRadioPropio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioPropioActionPerformed(evt);
            }
        });

        btnGroupOrigen.add(jRadioTercero);
        jRadioTercero.setText("Tercero");
        jRadioTercero.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioTerceroActionPerformed(evt);
            }
        });

        jLabel4.setLabelFor(jTextHora);
        jLabel4.setText("Hora");

        jTextHora.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter(java.text.DateFormat.getTimeInstance(java.text.DateFormat.SHORT))));

        jLabel5.setBackground(new java.awt.Color(153, 153, 153));
        jLabel5.setText("Tipo de producto");

        btnGroupTipoProducto.add(jRadioYerbaMateVerde);
        jRadioYerbaMateVerde.setText("Yerba Mate Verde");
        jRadioYerbaMateVerde.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioYerbaMateVerdeActionPerformed(evt);
            }
        });

        btnGroupTipoProducto.add(jRadioYerbaCanchada);
        jRadioYerbaCanchada.setText("Yerba Canchada");
        jRadioYerbaCanchada.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioYerbaCanchadaActionPerformed(evt);
            }
        });

        btnGroupTipoProducto.add(jRadioLeñaVerde);
        jRadioLeñaVerde.setText("Leña Verde");
        jRadioLeñaVerde.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioLeñaVerdeActionPerformed(evt);
            }
        });

        btnGroupTipoProducto.add(jRadioLeñaSeca);
        jRadioLeñaSeca.setText("Leña Seca");
        jRadioLeñaSeca.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioLeñaSecaActionPerformed(evt);
            }
        });

        jLabel6.setText("Chofer");

        jLabel7.setText("Vehículo");

        jComboVehiculo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboVehiculoActionPerformed(evt);
            }
        });

        jLabel9.setText("Humedad");

        jTextHumedad.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0"))));
        jTextHumedad.setText("0");
        jTextHumedad.setEnabled(false);
        jTextHumedad.setPreferredSize(new java.awt.Dimension(50, 19));
        jTextHumedad.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextHumedadActionPerformed(evt);
            }
        });
        jTextHumedad.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                jTextHumedadFocusLost(evt);
            }
        });

        jLabel10.setText("Palo");

        jTextPalo.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0"))));
        jTextPalo.setText("0");
        jTextPalo.setEnabled(false);
        jTextPalo.setPreferredSize(new java.awt.Dimension(50, 19));
        jTextPalo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextPaloActionPerformed(evt);
            }
        });
        jTextPalo.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                jTextPaloFocusLost(evt);
            }
        });

        jLabel11.setText("Polvo");

        jTextPolvo.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0"))));
        jTextPolvo.setText("0");
        jTextPolvo.setEnabled(false);
        jTextPolvo.setPreferredSize(new java.awt.Dimension(50, 19));
        jTextPolvo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextPolvoActionPerformed(evt);
            }
        });

        jSeparator2.setOrientation(javax.swing.SwingConstants.VERTICAL);

        jLabel12.setText("Kilaje Total");

        jLabel13.setText("Peso Bruto");

        jTextPesoBruto.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0"))));
        jTextPesoBruto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextPesoBrutoActionPerformed(evt);
            }
        });
        jTextPesoBruto.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                jTextPesoBrutoFocusLost(evt);
            }
        });

        jLabel14.setText("Tara");

        jTextTara.setEditable(false);

        jTextKilajeTotal.setEditable(false);
        jTextKilajeTotal.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextKilajeTotalActionPerformed(evt);
            }
        });

        jLabel15.setText("Tipo de Madera");

        jComboTipoMadera.setEnabled(false);

        jBtnCerrar.setText("Cerrar");
        jBtnCerrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnCerrarActionPerformed(evt);
            }
        });

        jBtnGuardar.setText("Guardar");
        jBtnGuardar.setEnabled(false);
        jBtnGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnGuardarActionPerformed(evt);
            }
        });

        jBtnNuevo.setText("Nuevo");
        jBtnNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnNuevoActionPerformed(evt);
            }
        });

        jLabel16.setText("Plantación");

        jComboPlantacion.setEnabled(false);
        jComboPlantacion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboPlantacionActionPerformed(evt);
            }
        });

        jLabel17.setText("Parcela");

        jComboParcela.setEnabled(false);

        jLabel8.setText("%");

        jLabel18.setText("%");

        jLabel19.setText("%");

        jLabel20.setText("Kg");

        jLabel21.setText("Kg");

        jLabel22.setText("Kg");

        jLabel23.setText("Cliente");

        jComboCliente.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jComboCliente.setEnabled(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel6)
                                .addComponent(jComboVehiculo, javax.swing.GroupLayout.PREFERRED_SIZE, 276, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel7)
                                .addComponent(jComboChofer, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 276, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel13)
                                    .addComponent(jLabel14)
                                    .addComponent(jLabel11)
                                    .addComponent(jLabel10))
                                .addGap(29, 29, 29)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jTextPesoBruto, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jTextTara, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jTextHumedad, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jTextPolvo, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jTextPalo, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jTextKilajeTotal, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel8)
                                    .addComponent(jLabel18)
                                    .addComponent(jLabel19)
                                    .addComponent(jLabel22)
                                    .addComponent(jLabel21)
                                    .addComponent(jLabel20))))
                        .addGap(26, 26, 26)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel15)
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addComponent(jSeparator3)))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 350, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel12)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jBtnNuevo)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jBtnGuardar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jBtnCerrar))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel4)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(jTextHora, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                            .addComponent(jLabel2)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                            .addComponent(dateFecha, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE))))
                                .addGap(136, 136, 136))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel9)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                        .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jComboTipoMadera, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jComboPlantacion, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jComboParcela, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel16)
                                    .addComponent(jLabel17))
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGap(0, 0, Short.MAX_VALUE)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jRadioYerbaCanchada)
                                    .addComponent(jRadioLeñaVerde)
                                    .addComponent(jRadioLeñaSeca)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(jRadioYerbaMateVerde, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 152, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addGap(22, 22, 22)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel3)
                                            .addComponent(jRadioPropio)
                                            .addComponent(jRadioTercero)
                                            .addComponent(jLabel23))
                                        .addGap(112, 112, 112))
                                    .addComponent(jComboCliente, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSeparator2)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(4, 4, 4)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel2)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                            .addComponent(jLabel4)
                                            .addComponent(jTextHora, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                    .addComponent(dateFecha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jLabel6)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jComboChofer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jLabel7)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jComboVehiculo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel13)
                                    .addComponent(jTextPesoBruto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel21))
                                .addGap(3, 3, 3)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jTextTara, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jLabel22))
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(3, 3, 3)
                                        .addComponent(jLabel14)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                            .addComponent(jLabel9)
                                            .addComponent(jTextHumedad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(jLabel8))))
                                .addGap(2, 2, 2))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel5)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jRadioYerbaMateVerde)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jRadioYerbaCanchada)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jRadioLeñaVerde)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jRadioLeñaSeca)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(jLabel15))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel3)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jRadioPropio)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jRadioTercero)
                                        .addGap(8, 8, 8)
                                        .addComponent(jLabel23)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jComboCliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jComboTipoMadera, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel16)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jComboPlantacion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(9, 9, 9)
                                .addComponent(jLabel17)
                                .addGap(1, 1, 1)))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jTextPolvo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel11)
                                    .addComponent(jLabel18))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jTextPalo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel10)
                                    .addComponent(jLabel19))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 12, Short.MAX_VALUE)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel12)
                                    .addComponent(jTextKilajeTotal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel20)))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(5, 5, 5)
                                .addComponent(jComboParcela, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jSeparator3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jBtnCerrar)
                                    .addComponent(jBtnGuardar)
                                    .addComponent(jBtnNuevo))))))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jRadioPropioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioPropioActionPerformed
        // TODO add your handling code here:
        this.jComboPlantacion.setEnabled(true);
        this.jComboParcela.setEnabled(true);
        this.jComboCliente.setEnabled(false);
        actualizarKilaje();
    }//GEN-LAST:event_jRadioPropioActionPerformed

    private void jTextHumedadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextHumedadActionPerformed
        // TODO add your handling code here:
        int humedad = Integer.parseInt(this.jTextHumedad.getText());
        if (humedad < 0) {
            this.jTextHumedad.setText("0");
        }
        if (humedad > 100) {
            this.jTextHumedad.setText("100");
        }
        actualizarKilaje();
    }//GEN-LAST:event_jTextHumedadActionPerformed

    private void jRadioYerbaMateVerdeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioYerbaMateVerdeActionPerformed
        // TODO add your handling code here:
        this.jTextHumedad.setEnabled(true);
        this.jTextPalo.setEnabled(true);
        this.jTextPolvo.setEnabled(false);
        this.jComboTipoMadera.setEnabled(false);
        if (this.jRadioTercero.isSelected()) {
            this.jComboCliente.setEnabled(true);
        } else {
            this.jComboCliente.setEnabled(false);
        }
        actualizarKilaje();
    }//GEN-LAST:event_jRadioYerbaMateVerdeActionPerformed

    private void jTextPaloActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextPaloActionPerformed
        // TODO add your handling code here:
        int palo = Integer.parseInt(this.jTextPalo.getText());
        if (palo < 0) {
            this.jTextPalo.setText("0");
        }
        if (palo > 100) {
            this.jTextPalo.setText("100");
        }
        actualizarKilaje();
    }//GEN-LAST:event_jTextPaloActionPerformed

    private void jBtnCerrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnCerrarActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_jBtnCerrarActionPerformed

    private void jRadioYerbaCanchadaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioYerbaCanchadaActionPerformed
        // TODO add your handling code here:
        this.jTextHumedad.setEnabled(false);
        this.jTextPalo.setEnabled(true);
        this.jTextPolvo.setEnabled(true);
        this.jComboTipoMadera.setEnabled(false);
        this.jComboCliente.setEnabled(false);
        this.jComboCliente.setEnabled(false);
        actualizarKilaje();
    }//GEN-LAST:event_jRadioYerbaCanchadaActionPerformed

    private void jRadioLeñaVerdeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioLeñaVerdeActionPerformed
        // TODO add your handling code here:
        this.jTextHumedad.setEnabled(true);
        this.jTextPalo.setEnabled(false);
        this.jTextPolvo.setEnabled(false);
        this.jComboTipoMadera.setEnabled(false);
        actualizarKilaje();
    }//GEN-LAST:event_jRadioLeñaVerdeActionPerformed

    private void jRadioLeñaSecaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioLeñaSecaActionPerformed
        // TODO add your handling code here:
        this.jTextHumedad.setEnabled(false);
        this.jTextPalo.setEnabled(false);
        this.jTextPolvo.setEnabled(false);
        this.jComboTipoMadera.setEnabled(true);
        this.jComboCliente.setEnabled(false);
        actualizarKilaje();
    }//GEN-LAST:event_jRadioLeñaSecaActionPerformed

    private void jRadioTerceroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioTerceroActionPerformed
        // TODO add your handling code here:
        this.jComboPlantacion.setEnabled(false);
        this.jComboParcela.setEnabled(false);
        if (this.jRadioYerbaMateVerde.isSelected()) {
            this.jComboCliente.setEnabled(true);
        } else {
            this.jComboCliente.setEnabled(false);
        }
        actualizarKilaje();
    }//GEN-LAST:event_jRadioTerceroActionPerformed

    private void inicializarCampos() {
        this.jTextHumedad.setEnabled(false);
        this.jTextPalo.setEnabled(false);
        this.jTextPolvo.setEnabled(false);
        this.jComboTipoMadera.setEnabled(false);
        this.dateFecha.setText(null);
        this.jTextHora.setText(null);
        this.jComboChofer.setSelectedIndex(0);
        this.jComboVehiculo.setSelectedIndex(0);
        this.jRadioLeñaSeca.setSelected(false);
        this.jRadioLeñaVerde.setSelected(false);
        this.jRadioYerbaCanchada.setSelected(false);
        this.jRadioYerbaMateVerde.setSelected(false);
        this.btnGroupTipoProducto.clearSelection();
        this.btnGroupOrigen.clearSelection();
        this.jRadioPropio.setSelected(false);
        this.jRadioTercero.setSelected(false);
        this.jTextHumedad.setText("0");
        this.jTextPalo.setText("0");
        this.jTextPolvo.setText("0");
        this.jComboPlantacion.setEnabled(false);
        this.jComboParcela.setEnabled(false);
        this.jComboTipoMadera.setSelectedIndex(0);
        this.jComboPlantacion.setSelectedIndex(0);
        this.jComboParcela.setSelectedIndex(0);
        this.jComboCliente.setEnabled(false);
        this.jTextPesoBruto.setText(null);
        this.jTextTara.setText(null);
        this.jTextKilajeTotal.setText(null);
    }

    private void jBtnNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnNuevoActionPerformed
        // TODO add your handling code here:
        inicializarCampos();
        this.jBtnNuevo.setEnabled(false);
        this.jBtnGuardar.setEnabled(true);
    }//GEN-LAST:event_jBtnNuevoActionPerformed

    private void jComboVehiculoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboVehiculoActionPerformed
        // TODO add your handling code here:
        Agricola unVehiculo = (Agricola) this.jComboVehiculo.getSelectedItem();
        this.jTextTara.setText(Integer.toString(unVehiculo.getTara()));
        actualizarKilaje();
    }//GEN-LAST:event_jComboVehiculoActionPerformed

    private void actualizarKilaje() {
        String vacio = "";
        int tara = 0;
        int pesobruto = 0;
        if (!this.jTextPesoBruto.getText().equals(vacio)) {
            if (!this.jTextTara.getText().equals(vacio)) {
                tara = Integer.parseInt(this.jTextTara.getText());
            }
            if (!this.jTextPesoBruto.getText().equals(vacio)) {
                pesobruto = Integer.parseInt(this.jTextPesoBruto.getText());
            }
            int kilaje = pesobruto - tara;

            int descuento = 0;
            int totalDescuento = 0;

            if (this.jTextHumedad.isEnabled()) {
                if (!this.jTextHumedad.getText().isEmpty()) {
                    int humedad = Integer.parseInt(this.jTextHumedad.getText());
                    descuento += (humedad * kilaje) / 100;
                    totalDescuento += humedad;
                }
            }
            if (this.jTextPalo.isEnabled()) {
                if (!this.jTextPalo.getText().isEmpty()) {
                    int palo = Integer.parseInt(this.jTextPalo.getText());
                    descuento += (palo * kilaje) / 100;
                    totalDescuento += palo;
                }
            }
            if (this.jTextPolvo.isEnabled()) {
                if (!this.jTextPolvo.getText().isEmpty()) {
                    int polvo = Integer.parseInt(this.jTextPolvo.getText());
                    descuento += (polvo * kilaje) / 100;
                    totalDescuento += polvo;
                }
            }

            if (totalDescuento > 100) {
                JOptionPane.showMessageDialog(this, "La suma de los descuentos no puede exceder el 100%");
                this.jTextPolvo.setText("0");
                this.jTextPalo.setText("0");
                this.jTextHumedad.setText("0");
                descuento = 0;
            }
            kilaje = kilaje - descuento;

            this.jTextKilajeTotal.setText(Integer.toString(kilaje));
        }
    }

    private void jComboPlantacionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboPlantacionActionPerformed
        // TODO add your handling code here:
        Plantacion laPlantacion = (Plantacion) this.jComboPlantacion.getSelectedItem();

        this.parcelas.removeAllElements();
        if (laPlantacion.getParcelas().size() > 0) { //Si La lista no tiene elementos
            Iterator it = laPlantacion.getParcelas().iterator();
            while (it.hasNext()) {
                this.parcelas.addElement((Parcela) it.next());
            }
        } else {
            //this.choferes.addElement("No se encontró ningún personal.");
        }
    }//GEN-LAST:event_jComboPlantacionActionPerformed

    private void jTextKilajeTotalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextKilajeTotalActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextKilajeTotalActionPerformed

    private void jTextPesoBrutoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextPesoBrutoActionPerformed
        // TODO add your handling code here:
        actualizarKilaje();
    }//GEN-LAST:event_jTextPesoBrutoActionPerformed

    private void jTextPesoBrutoFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextPesoBrutoFocusLost
        // TODO add your handling code here:
        actualizarKilaje();
    }//GEN-LAST:event_jTextPesoBrutoFocusLost

    private void jBtnGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnGuardarActionPerformed
        // TODO add your handling code here:
        //verificar campos obligatorios
        String vacio = "";
        boolean sePuedeGuardar = true;
        String lugar = "";
        Seccion unaSeccion = null;
        if (this.jRadioYerbaMateVerde.isSelected()) {
            unaSeccion = this.miEmpresa.buscarSeccionDisponible(Integer.parseInt(this.jTextKilajeTotal.getText()));
            if (unaSeccion == null) {
                sePuedeGuardar = false;
                lugar = "sección";
            }
        }
        Deposito unDeposito = null;
        if (this.jRadioYerbaCanchada.isSelected()) {
            unDeposito = this.miEmpresa.buscarDepositoDisponible(Integer.parseInt(this.jTextKilajeTotal.getText()));
            if (unDeposito == null) {
                sePuedeGuardar = false;
                lugar = "depósito";
            }
        }
        if (sePuedeGuardar) {
            if (this.formErrores().equals(vacio)) {
                try {
                    this.dateFecha.setFormat(3);
                    Date fecha = Empresa.miUtilidadDeFechas.fechaDb(this.dateFecha.getText());
                    Time hora = Time.valueOf(this.jTextHora.getText() + ":00");
                    Personal chofer = (Personal) this.jComboChofer.getSelectedItem();
                    Agricola vehiculo = (Agricola) this.jComboVehiculo.getSelectedItem();
                    Parcela parcela = (Parcela) this.jComboParcela.getSelectedItem();
                    Cliente cliente = (Cliente) this.jComboCliente.getSelectedItem();
                    int pesoBruto = Integer.parseInt(this.jTextPesoBruto.getText());
                    int pesoTara = Integer.parseInt(this.jTextTara.getText());
                    int kilaje = Integer.parseInt(this.jTextKilajeTotal.getText());
                    String origen = "";
                    if (this.jRadioPropio.isSelected()) {
                        origen = "propio";
                    }
                    if (this.jRadioTercero.isSelected()) {
                        origen = "tercero";
                    }

                    if (this.jRadioYerbaMateVerde.isSelected()) {
                        int humedad = Integer.parseInt(this.jTextHumedad.getText());
                        int palo = Integer.parseInt(this.jTextPalo.getText());
                        if (this.jComboCliente.isEnabled()) {
                            this.miEmpresa.altaIngresoYerbaMateVerde(fecha, hora, chofer, vehiculo, pesoBruto,
                                    pesoTara, humedad, palo, parcela, origen, kilaje, unaSeccion, cliente);
                        } else {
                            this.miEmpresa.altaIngresoYerbaMateVerde(fecha, hora, chofer, vehiculo, pesoBruto,
                                    pesoTara, humedad, palo, parcela, origen, kilaje, unaSeccion);
                        }
                    }

                    if (this.jRadioYerbaCanchada.isSelected()) {
                        int palo = Integer.parseInt(this.jTextPalo.getText());
                        int polvo = Integer.parseInt(this.jTextPolvo.getText());
                        this.miEmpresa.altaIngresoYerbaCanchada(fecha, hora, chofer, vehiculo, pesoBruto,
                                pesoTara, polvo, palo, parcela, origen, kilaje, unDeposito);
                    }

                    if (this.jRadioLeñaSeca.isSelected()) {
                        TipoMadera tipoMadera = (TipoMadera) this.jComboTipoMadera.getSelectedItem();
                        this.miEmpresa.altaIngresoLeniaSeca(fecha, hora, chofer, vehiculo, pesoBruto,
                                pesoTara, tipoMadera, parcela, origen, kilaje);
                    }

                    if (this.jRadioLeñaVerde.isSelected()) {
                        int humedad = Integer.parseInt(this.jTextHumedad.getText());
                        this.miEmpresa.altaIngresoLeniaVerde(fecha, hora, chofer, vehiculo, pesoBruto,
                                pesoTara, humedad, parcela, origen, kilaje);
                    }

                    JOptionPane.showMessageDialog(this, "Se ha realizado el ingreso con éxito");
                    this.jBtnGuardar.setEnabled(false);
                    this.jBtnNuevo.setEnabled(true);
                    cargarIngresos();

                } catch (MateriaPrimaException mpex) {
                    JOptionPane.showMessageDialog(this, "Error: No se pudo realizar el Ingreso");
                } catch (Exception ex) {
                    JOptionPane.showMessageDialog(this, "Error: " + ex.getMessage());
                }

            } else {
                JOptionPane.showMessageDialog(this, this.formErrores());
            }
        } else {
            JOptionPane.showMessageDialog(this, "No hay " + lugar + " con capacidad disponible");
        }
    }//GEN-LAST:event_jBtnGuardarActionPerformed

    private void jTextHumedadFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextHumedadFocusLost
        // TODO add your handling code here:
        int humedad = Integer.parseInt(this.jTextHumedad.getText());
        if (humedad < 0) {
            this.jTextHumedad.setText("0");
        }
        if (humedad > 100) {
            this.jTextHumedad.setText("100");
        }
        actualizarKilaje();
    }//GEN-LAST:event_jTextHumedadFocusLost

    private void jTextPolvoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextPolvoActionPerformed
        // TODO add your handling code here:
        int polvo = Integer.parseInt(this.jTextPolvo.getText());
        if (polvo < 0) {
            this.jTextPolvo.setText("0");
        }
        if (polvo > 100) {
            this.jTextPolvo.setText("100");
        }
        actualizarKilaje();
    }//GEN-LAST:event_jTextPolvoActionPerformed

    private void jTextPaloFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextPaloFocusLost
        // TODO add your handling code here
        int palo = Integer.parseInt(this.jTextPalo.getText());
        if (palo < 0) {
            this.jTextPalo.setText("0");
        }
        if (palo > 100) {
            this.jTextPalo.setText("100");
        }
        actualizarKilaje();
    }//GEN-LAST:event_jTextPaloFocusLost

    private void jListIngresosValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_jListIngresosValueChanged
        // TODO add your handling code here:
        inicializarCampos();

        this.unIngreso = (Ingreso) this.ingresos.getElementAt(this.jListIngresos.getSelectedIndex());
        this.dateFecha.setText(Empresa.miUtilidadDeFechas.fechaAr(this.unIngreso.getFecha()));
        this.jTextHora.setText(this.unIngreso.getHora().toString());
        String origen = this.unIngreso.getMateriaPrima().getOrigen();
        if (origen.equals("propio")) {
            this.jRadioPropio.setSelected(true);
        }
        if (origen.equals("tercero")) {
            this.jRadioTercero.setSelected(true);
        }
        this.jComboParcela.setSelectedItem(this.unIngreso.getMateriaPrima().getParcela());
        this.jComboChofer.setSelectedItem(this.unIngreso.getChofer());
        this.jComboVehiculo.setSelectedItem(this.unIngreso.getVehiculo());
        this.jTextPesoBruto.setText(String.valueOf(this.unIngreso.getPesoBruto()));
        this.jTextTara.setText(String.valueOf(this.unIngreso.getPesoTara()));
        this.jTextKilajeTotal.setText(Integer.toString(this.unIngreso.getMateriaPrima().getKilaje()));

        //System.out.println(this.unIngreso.getMateriaPrima().getClass().getName());
        if (this.unIngreso.getMateriaPrima() instanceof YerbaMateVerde) {
            YerbaMateVerde ymv = (YerbaMateVerde) this.unIngreso.getMateriaPrima();
            this.jTextHumedad.setText(Integer.toString(ymv.getHumedad()));
            this.jTextPalo.setText(Integer.toString(ymv.getPalo()));
            this.jRadioYerbaMateVerde.setSelected(true);
        }

        if (this.unIngreso.getMateriaPrima() instanceof YerbaMateCanchada) {
            YerbaMateCanchada ymc = (YerbaMateCanchada) this.unIngreso.getMateriaPrima();
            this.jTextPalo.setText(Integer.toString(ymc.getPalo()));
            this.jTextPolvo.setText(Integer.toString(ymc.getPolvo()));
            this.jRadioYerbaCanchada.setSelected(true);
        }

        if (this.unIngreso.getMateriaPrima() instanceof LeniaSeca) {
            LeniaSeca ls = (LeniaSeca) this.unIngreso.getMateriaPrima();
            this.jComboTipoMadera.setSelectedItem(ls.getTipoMadera());
            this.jRadioLeñaSeca.setSelected(true);
        }

        if (this.unIngreso.getMateriaPrima() instanceof LeniaVerde) {
            LeniaVerde lv = (LeniaVerde) this.unIngreso.getMateriaPrima();
            this.jTextHumedad.setText(Integer.toString(lv.getHumedad()));
            this.jRadioLeñaVerde.setSelected(true);
        }

        this.jBtnGuardar.setEnabled(false);
        this.jBtnNuevo.setEnabled(true);

        //String tipo = this.unIngreso.getMateriaPrima().get
    }//GEN-LAST:event_jListIngresosValueChanged

    private void jTextPolvoFocusLost(java.awt.event.FocusEvent evt) {
        // TODO add your handling code here:
        int polvo = Integer.parseInt(this.jTextPolvo.getText());
        if (polvo < 0) {
            this.jTextPolvo.setText("0");
        }
        if (polvo > 100) {
            this.jTextPolvo.setText("100");
        }
        actualizarKilaje();
    }

    private String formErrores() {
        String mensaje = "";
        if (this.jTextHora.getText().isEmpty()) {
            mensaje += " No ingresó Hora |";
        }
        if (this.jComboChofer.getSelectedIndex() == -1) {
            mensaje += " No seleccionó Chofer |";
        }
        if (this.jComboVehiculo.getSelectedIndex() == -1) {
            mensaje += " No seleccionó Vehículo |";
        }
        if (this.jTextPesoBruto.getText().isEmpty()) {
            mensaje += " No ingresó Peso Bruto |";
        } else {
            int pesoBruto = Integer.parseInt(this.jTextPesoBruto.getText());
            if (pesoBruto <= 0) {
                mensaje += " Peso Bruto debe tener un valor mayor a 0 |";
            }
            if (!this.jTextTara.getText().isEmpty()) {
                int tara = Integer.parseInt(this.jTextTara.getText());
                if (tara > pesoBruto) {
                    mensaje += " Tara no puede ser mayor al peso bruto |";
                }
            }
        }
        if (this.jTextHumedad.isEnabled()) {
            if (this.jTextHumedad.getText().isEmpty()) {
                mensaje += " No ingresó Humedad |";
            } else {
                int humedad = Integer.parseInt(this.jTextHumedad.getText());
                if (humedad < 0 || humedad > 100) {
                    mensaje += " Humedad debe tener un valor entre 0 y 100 |";
                }
            }
        }
        if (this.jTextPolvo.isEnabled()) {
            if (this.jTextPolvo.getText().isEmpty()) {
                mensaje += " No ingresó Polvo |";
            } else {
                int polvo = Integer.parseInt(this.jTextPolvo.getText());
                if (polvo < 0 || polvo > 100) {
                    mensaje += " Polvo debe tener un valor entre 0 y 100 |";
                }
            }
        }
        if (this.jTextPalo.isEnabled()) {
            if (this.jTextPalo.getText().isEmpty()) {
                mensaje += " No ingresó Palo |";
            } else {
                int palo = Integer.parseInt(this.jTextPalo.getText());
                if (palo < 0 || palo > 100) {
                    mensaje += " Palo debe tener un valor entre 0 y 100 |";
                }
            }
        }
        boolean tipoProductoSeleccionado = false;
        for (Enumeration<AbstractButton> buttons = btnGroupTipoProducto.getElements(); buttons.hasMoreElements();) {
            AbstractButton button = buttons.nextElement();
            if (button.isSelected()) {
                tipoProductoSeleccionado = true;
            }
        }
        if (!tipoProductoSeleccionado) {
            mensaje += " No seleccionó Tipo de Producto |";
        }
        boolean tipoOrigenSeleccionado = false;
        for (Enumeration<AbstractButton> buttons = btnGroupOrigen.getElements(); buttons.hasMoreElements();) {
            AbstractButton button = buttons.nextElement();
            if (button.isSelected()) {
                tipoOrigenSeleccionado = true;
            }
        }
        if (!tipoOrigenSeleccionado) {
            mensaje += " No seleccionó Origen |";
        }
        if (this.jComboTipoMadera.isEnabled()) {
            if (this.jComboTipoMadera.getSelectedIndex() == -1) {
                mensaje += " No seleccionó Tipo de Madera |";
            }
        }
        if (this.jComboParcela.isEnabled()) {
            if (this.jComboParcela.getSelectedIndex() == -1) {
                mensaje += " No seleccionó Parcela |";
            }
        }
        if (!mensaje.isEmpty()) {
            mensaje = "Se encontraron los siguientes errores: " + mensaje;
        }
        return mensaje;
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(DlgIngreso.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                DlgIngreso dialog = new DlgIngreso(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup btnGroupOrigen;
    private javax.swing.ButtonGroup btnGroupTipoProducto;
    private datechooser.beans.DateChooserCombo dateFecha;
    private javax.swing.JButton jBtnCerrar;
    private javax.swing.JButton jBtnGuardar;
    private javax.swing.JButton jBtnNuevo;
    private javax.swing.JComboBox jComboChofer;
    private javax.swing.JComboBox jComboCliente;
    private javax.swing.JComboBox jComboParcela;
    private javax.swing.JComboBox jComboPlantacion;
    private javax.swing.JComboBox jComboTipoMadera;
    private javax.swing.JComboBox jComboVehiculo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel20;
    private javax.swing.JLabel jLabel21;
    private javax.swing.JLabel jLabel22;
    private javax.swing.JLabel jLabel23;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JList jListIngresos;
    private javax.swing.JRadioButton jRadioLeñaSeca;
    private javax.swing.JRadioButton jRadioLeñaVerde;
    private javax.swing.JRadioButton jRadioPropio;
    private javax.swing.JRadioButton jRadioTercero;
    private javax.swing.JRadioButton jRadioYerbaCanchada;
    private javax.swing.JRadioButton jRadioYerbaMateVerde;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JFormattedTextField jTextHora;
    private javax.swing.JFormattedTextField jTextHumedad;
    private javax.swing.JFormattedTextField jTextKilajeTotal;
    private javax.swing.JFormattedTextField jTextPalo;
    private javax.swing.JFormattedTextField jTextPesoBruto;
    private javax.swing.JFormattedTextField jTextPolvo;
    private javax.swing.JTextField jTextTara;
    // End of variables declaration//GEN-END:variables
}
